Skip to content

[SPARK-57846][SQL] Add the try_make_time function#56932

Open
yadavay-amzn wants to merge 4 commits into
apache:masterfrom
yadavay-amzn:SPARK-57846
Open

[SPARK-57846][SQL] Add the try_make_time function#56932
yadavay-amzn wants to merge 4 commits into
apache:masterfrom
yadavay-amzn:SPARK-57846

Conversation

@yadavay-amzn

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Adds the try_make_time(hour, minute, second) function, the error-safe counterpart to make_time: it returns the same result on valid input and NULL (instead of throwing) on invalid input. It is implemented as TryEval(MakeTime(...)), mirroring try_make_timestamp. Registered in the function registry, exposed via functions.scala, and documented (ANSI-compliance try-functions list + sql-expression-schema).

Why are the changes needed?

make_time throws on invalid components (e.g. hour = 25). Users want a null-returning variant consistent with the other try_* datetime constructors (try_make_timestamp, etc.).

Does this PR introduce any user-facing change?

Yes - a new function try_make_time.

How was this patch tested?

Unit tests (TimeExpressionsSuite), integration (TimeFunctionsSuiteBase), SQL-level (time.sql), and Connect plan-generation goldens: valid input equals make_time, invalid input returns NULL.

Was this patch authored or co-authored using generative AI tooling?

Authored with assistance by Claude Opus 4.8.

@MaxGekk MaxGekk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 blocking, 0 non-blocking, 1 nits.
Clean, faithful mirror of the try_* datetime-constructor pattern (TryEval(MakeTime(...)), like try_to_time). Implementation, registry, API surface, docs, and end-to-end tests are all correct and consistent. One blocking versioning fix.

Correctness (1)

  • timeExpressions.scala:645, functions.scala:904, builtin.py:25589: @since/versionadded is 4.1.0, but v4.1.0 is already released and does not contain this new function — should be branch-4.x's 4.3.0. See inline.

Nits: 1 minor item (see inline comments).

NULL
""",
group = "datetime_funcs",
since = "4.1.0")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_make_time is a new function, but v4.1.0 is already released and does not contain it (it's absent from the v4.1.0 function registry). since should be the version this first ships in — branch-4.x = 4.3.0 (make_time/try_to_time legitimately say 4.1.0 because they shipped in that release; the value was likely copied from make_time).

Suggested change
since = "4.1.0")
since = "4.3.0")

If this is intended to be master-only (unusual for a new user-facing function), use 5.0.0 instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed - updated since to 4.3.0 in all three locations (ExpressionDescription, Scala API @SInCE, PySpark versionadded), since try_make_time is new in branch-4.x and absent from the released 4.1.0. Also changed the unit test to construct TryMakeTime(...) directly so the new expression/builder is exercised.

* @param second
* the second to represent, from 0 to 59.999999
* @group datetime_funcs
* @since 4.1.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same versioning issue — update the Scala API @since to match (4.3.0, the branch-4.x version this ships in).

Suggested change
* @since 4.1.0
* @since 4.3.0

Comment thread python/pyspark/sql/functions/builtin.py Outdated
Try to create time from hour, minute and second fields.
The function returns NULL on invalid inputs.

.. versionadded:: 4.1.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same versioning issue — update the Python versionadded to match (4.3.0). (The Connect builtin.py forwards __doc__, so no separate change is needed there.)

Suggested change
.. versionadded:: 4.1.0
.. versionadded:: 4.3.0

)
}

test("creating values of TimeType via try_make_time") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (non-blocking): this test builds TryEval(MakeTime(...)) directly, so despite its title it doesn't instantiate the new TryMakeTime expression or TryMakeTimeExpressionBuilder. The class/builder are covered end-to-end by TimeFunctionsSuiteBase and the SQL golden, so coverage isn't missing — but constructing new TryMakeTime(...) here would make the unit test match its title and directly cover the new expression's plumbing (including the builder's arg-count error path, which nothing currently exercises).

@yadavay-amzn

Copy link
Copy Markdown
Contributor Author

@MaxGekk Addressed the review comments: added a TryMakeTime wrapper so the display name is try_make_time, added the PySpark binding, fixed the doctest, and set the version to 4.3.0. Could you take another look when you get a chance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants